package cn.wolfcode.web.controller;


import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.qo.UserContext;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/consumptionItem")
public class ConsumptionItemController {

    @Autowired
    private IConsumptionItemService consumptionItemService;


    @RequiredPermission(name = "结算单明细新增/编辑", expression = "consumptionItem:saveOrUpdate")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<ConsumptionItem> pageInfo = consumptionItemService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "consumptionItem/list";
    }

    @RequestMapping("/delete")
    @RequiredPermission(name = "结算单明细删除", expression = "consumptionItem:delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        if (id != null) {
            consumptionItemService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "结算单明细新增/编辑", expression = "consumptionItem:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(ConsumptionItem consumptionItem) {
        //如果增加的时候没有回显 因为没有流水号 没有流水号就回显不了上面的数据
        //因为丢失了数据 所以我们要把数据补上 在form里面写一个hidden 因为我们是根据流水号来回显数据
        consumptionItem.setCreateTime(new Date());
        consumptionItem.setCreateUser(UserContext.getEmployee());
        consumptionItemService.save(consumptionItem);
        return new JsonResult();
    }
}
